package com.mb.mp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mb.common.utils.DateUtils;
import com.mb.mp.dao.*;
import com.mb.mp.domain.MpMemberCommissionDtlDO;
import com.mb.mp.domain.MpMemberInfoDO;
import com.mb.mp.domain.MpProductInfoDO;
import com.mb.mp.domain.MpUserInfoDO;
import com.mb.mp.service.IMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品管理服务实现
 *
 * @author
 * @create 2018-09-19 下午3:34
 **/
@Service("memberService")
public class MemberServiceImpl implements IMemberService {
    private static Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Autowired
    private MpMemberInfoDOMapper memberInfoDOMapper;

    @Autowired
    private MpUserInfoDOMapper userInfoDOMapper;

    @Autowired
    private MpSysConfigDOMapper sysConfigDOMapper;

    @Autowired
    private MpMemberCommissionDtlDOMapper memberCommissionDtlDOMapper;

    @Autowired
    private MpProductInfoDOMapper productInfoDOMapper;

    @Override
    public List<MpMemberInfoDO> queryList(Map<String, Object> map) {
        List<MpMemberInfoDO> list = memberInfoDOMapper.list(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return memberInfoDOMapper.count(map);
    }


    @Override
    public List<MpUserInfoDO> queryUserList(Map<String, Object> map) {
        return userInfoDOMapper.getList(map);
    }

    @Override
    public int userCount(Map<String, Object> map) {
        return userInfoDOMapper.count(map);
    }

    @Transactional
    @Override
    public int gain(MpUserInfoDO userInfoDO) {
        BigDecimal commission = new BigDecimal(0);
        MpUserInfoDO mpUserInfoDO = userInfoDOMapper.selectByPrimaryKey(userInfoDO.getUserId());
        if(mpUserInfoDO != null){
            Long productId = mpUserInfoDO.getInviteProductId();
            MpProductInfoDO productInfoDO = productInfoDOMapper.selectByPrimaryKey(productId);
            if(productInfoDO != null){
                commission = productInfoDO.getCommission();
                if(commission.compareTo(new BigDecimal(0)) <=0 ){
                    logger.info("产品佣金异常,productId :"+productId);
                    return 0;
                }
            }
        }

        int isGainCommission = userInfoDO.getIsGainCommission() == null ?
                0 : userInfoDO.getIsGainCommission().intValue();
        if(isGainCommission == 1){
            MpMemberCommissionDtlDO memberCommissionDtl = new MpMemberCommissionDtlDO();
            memberCommissionDtl.setMemberId(userInfoDO.getMemberId());
            memberCommissionDtl.setUserId(userInfoDO.getUserId());
            memberCommissionDtl.setCommissionAmt(commission);
            memberCommissionDtlDOMapper.insertSelective(memberCommissionDtl);

            MpMemberInfoDO memberInfoDO = new MpMemberInfoDO();
            memberInfoDO.setMemberId(userInfoDO.getMemberId());
            memberInfoDO.setTotalCommissionAmt(commission);
            memberInfoDO.setRemainCommissionAmt(commission);
            memberInfoDOMapper.updateCommission(memberInfoDO);

            MpUserInfoDO userInfo = new MpUserInfoDO();
            userInfo.setUserId(userInfoDO.getUserId());
            userInfo.setIsGainCommission(new Integer(1));
            userInfoDOMapper.updateByPrimaryKeySelective(userInfo);
        }else if(isGainCommission == 2){
            MpUserInfoDO userInfo = new MpUserInfoDO();
            userInfo.setUserId(userInfoDO.getUserId());
            userInfo.setIsGainCommission(new Integer(2));
            userInfoDOMapper.updateByPrimaryKeySelective(userInfo);
        }
        return 1;
    }

    @Transactional
    @Override
    public int cancel(MpUserInfoDO userInfoDO) {
        BigDecimal commission = new BigDecimal(0);

        MpUserInfoDO userInfo = userInfoDOMapper.selectByPrimaryKey(userInfoDO.getUserId());
        if(userInfo != null && userInfo.getIsGainCommission().intValue() == 1){
            Long memberId = userInfo.getMemberId();
            MpMemberCommissionDtlDO dtl = new MpMemberCommissionDtlDO();
            dtl.setMemberId(memberId);
            dtl.setUserId(userInfo.getUserId());
            MpMemberCommissionDtlDO dtlDO = memberCommissionDtlDOMapper.getMemberCommission(dtl);
            if(dtlDO != null){
                commission = dtlDO.getCommissionAmt();

                //删除佣金记录
                MpMemberCommissionDtlDO commissionDtlDO =  new MpMemberCommissionDtlDO();
                commissionDtlDO.setId(dtlDO.getId());
                commissionDtlDO.setDataStatus(new Integer(0));
                memberCommissionDtlDOMapper.updateByPrimaryKeySelective(commissionDtlDO);

                //扣减佣金
                BigDecimal newCommission = new BigDecimal(0).subtract(commission);

                MpMemberInfoDO memberInfoDO = new MpMemberInfoDO();
                memberInfoDO.setMemberId(userInfoDO.getMemberId());
                memberInfoDO.setTotalCommissionAmt(newCommission);
                memberInfoDO.setRemainCommissionAmt(newCommission);
                memberInfoDOMapper.updateCommission(memberInfoDO);

                MpUserInfoDO userDO = new MpUserInfoDO();
                userDO.setUserId(userInfoDO.getUserId());
                userDO.setIsGainCommission(new Integer(3));
                userInfoDOMapper.updateByPrimaryKeySelective(userDO);
            }
        }
        return 1;
    }

    @Override
    public BigDecimal getCommission(Long memberId, String month) {
        MpMemberCommissionDtlDO memberCommissionDtl = new MpMemberCommissionDtlDO();
        memberCommissionDtl.setMemberId(memberId);
        memberCommissionDtl.setYearMonth(month);
        return memberCommissionDtlDOMapper.getCommission(memberCommissionDtl);
    }

    @Override
    public JSONObject commissionStats(MpMemberCommissionDtlDO memberCommissionDtlDO) {
        Map<String,Object> map = new HashMap<>();
        map.put("memberId",memberCommissionDtlDO.getMemberId());
        map.put("yearMonth",memberCommissionDtlDO.getYearMonth());
        List<Map<String, Object>> rows = memberCommissionDtlDOMapper.statsOrderQTYOfDaysByMonth(map);

        JSONArray columns = new JSONArray();

        columns.fluentAdd("date").fluentAdd("QTY");

        return new JSONObject().fluentPut("columns", columns).fluentPut("rows", rows);
    }

    @Override
    public MpMemberInfoDO getMember(Long memberId) {
        return memberInfoDOMapper.selectByPrimaryKey(memberId);
    }

    @Override
    public int updateAliapyAccount(MpMemberInfoDO memberInfo) {
        Long memberId = memberInfo.getMemberId();
        MpMemberInfoDO memberInfoDO = memberInfoDOMapper.selectByPrimaryKey(memberId);
        if(memberInfoDO != null){
            MpMemberInfoDO member = new MpMemberInfoDO();
            member.setMemberId(memberId);
            member.setAlipayAccount(memberInfo.getAlipayAccount());
            return memberInfoDOMapper.updateByPrimaryKeySelective(member);
        }
        return 0;
    }


    @Override
    public int setVip(MpMemberInfoDO memberInfo) {
        Long memberId = memberInfo.getMemberId();
        MpMemberInfoDO memberInfoDO = memberInfoDOMapper.selectByPrimaryKey(memberId);
        if(memberInfoDO != null){
            memberInfoDO.setIsVip(1);
            Integer vipLevel = memberInfo.getVipLevel();
            memberInfoDO.setVipLevel(vipLevel);
            Date expireDate = null;
            if(vipLevel != null && vipLevel.intValue() == 1){
                //年VIP
                expireDate = DateUtils.getAfterYearDate(1);
            }else if(vipLevel != null && vipLevel.intValue() == 2){
                //终生VIP
                expireDate = null;
            }
            memberInfoDO.setRechargeTime(new Date());
            memberInfoDO.setExpireDate(expireDate);

            return memberInfoDOMapper.updateByPrimaryKey(memberInfoDO);
        }
        return 0;
    }

    @Override
    public int cancelVip(MpMemberInfoDO memberInfo) {
        Long memberId = memberInfo.getMemberId();
        MpMemberInfoDO memberInfoDO = memberInfoDOMapper.selectByPrimaryKey(memberId);
        if(memberInfoDO != null){
            memberInfoDO.setIsVip(0);
            memberInfoDO.setVipLevel(null);
            memberInfoDO.setRechargeTime(null);
            memberInfoDO.setExpireDate(null);
            return memberInfoDOMapper.updateByPrimaryKey(memberInfoDO);
        }
        return 0;
    }


    @Override
    public int removeMember(MpMemberInfoDO memberInfo) {
        Long memberId = memberInfo.getMemberId();
        MpMemberInfoDO memberInfoDO = memberInfoDOMapper.selectByPrimaryKey(memberId);
        if(memberInfoDO != null){
            return memberInfoDOMapper.deleteByPrimaryKey(memberId);
        }
        return 0;
    }
}
